const {Category} = require("../models/categoryModel.js");
const time = require("moment");
//引入图片文件上传相关处理插件
const { moveFiles, removeFiles } = require("../utils/upload.js");


async function showCategorys(req, res, next) {
    const { type='categoryName', value='', pageSize=10, currentPage=1 } = req.query;
    //总条数,获取学生模型总数据条数
    const totalNum = await Category.countDocuments();
    //总页数
    const totalPages = Math.ceil(totalNum / pageSize);
    //模糊查询
    let data= await Category
    // .find()
    // .limit(pageSize - 0)//设置请求的条数
    // .skip((currentPage - 1) * pageSize)// 设置跳过的条数
    // console.log(data1,"data111111");
    // let data = await Category
    .find({ [type]: { $regex: value, $options: "$i" } })
    .limit(pageSize - 0)//设置请求的条数
    .skip((currentPage - 1) * pageSize)// 设置跳过的条数
    // .populate({
    //     path: "productId",
    // })
    console.log(data,"获取保险产品数据");
    if(data){
        res.send({
            status: 1,
            data:  { pageSize, totalNum, totalPages, currentPage, data, },
        })
    }else{
        res.send({
            status: 0,
            message:"产品数据获取失败哦~"
        })
    }

};
//新增保险产品
async function addCategorys(req, res, next) {
    console.log(req.body,"获取页面新增的保险产品数据");
    const {categoryName,state} = req.body;
    console.log();
    try {
        let data = await Category.create({categoryName:categoryName,state:state,});
        console.log(data,999999);
        res.send({
            status: 1,
            data: data,
        })
    } catch (error) {
        res.send({
            status: 0,
            message:"新增保险产品数据输入无效",
        })
    }
    
}
//删除产品信息
async function deleteCategorys(req, res, next) {
    const { _id} =req.body;
    console.log(req.body);
    const id = await Category.find({_id:_id});
    if(id){
        let data1 = await Category.deleteOne({_id});
        if(data1.deletedCount==1){
            let data = await Category.find();
            res.send({
                message: "该保险产品删除成功",
                status: 1,
                data: data,
            })
        }
    }else {
        res.send({
            message: "id获取失败",
            status: 0,
        })
    } 
}
 //修改保险信息---获取要修改的数据的id
 async function getCategorys(req, res, next) {
    const stuid = await Category.find({ _id: req.params._id });
    if (stuid) {
        res.send({
            message: "保险产品数据id获取成功",
            status: 1,
            data: stuid,
        })
    } else {
        res.send({
            status: 0,
            message: "保险产品数据id获取失败",
        })
    }
    
}
//修改保险信息---点击确认修改的数据
async function updateCategorys(req, res, next) {
    try {
        const { _id, categoryName,} = req.body;
        const stuid = await Category.find({ _id: _id });
            const thestu = await Category.updateOne({ _id: _id }, { categoryName: categoryName, });
            console.log(thestu,"修改结果");
            if (thestu.nModified == 1) {
                let data = await Category.find();
                res.send({
                    message: "修改成功",
                    status: 1,
                    data: data,
                })
            }else {
                res.send({
                    status: 0,
                    message: "修改失败",
                })
            }
        
    } catch (error) {
        console.log('修改失败', error);
        res.send({
            status: 0,
            message: '修改失败'
        })
    }
}
module.exports = { showCategorys, getCategorys,addCategorys,updateCategorys,deleteCategorys }